import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
	state: {
		user: {
			isLogin: false
		},
		errorList: [],
		addressList: [],
		baseUrl: ""
	},
	mutations: {
		fetchCurrentUser(state, rawData) {
			state.user = {
				isLogin: true,
				...rawData
			}
		},
		setUrl(state, baseUrl) {
			state.baseUrl = baseUrl
		},
		clearUser(state) {
			state.user = {
				isLogin: false,
			}
		},
		addAddress(state, val) {
			state.addressList.push(val)
		}
	},
	getters: {
		getUser(state) {
			return state.user
		},
		getUrl(state) {
			return state.baseUrl
		},
	},
	actions: {
		getIP({
			commit
		}, projectCode) {
			return new Promise((resolve, reject) => {
				uni.request({
					url: "http://120.195.25.226:82/chne-totalbackstage/app/queryProjectServerIp",
					// url: "http://192.168.2.17:8081/app/queryProjectServerIp",
					method: "GET",
					data: {
						projectCode: projectCode
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
					},
					dataType: 'json',
					success: (res) => {
						console.log(res, 'test');
						if (res.data.status == 200) {
							console.log(res.data.object.serverIp)
							// resolve(res.data)
							commit("setUrl", res.data.object.serverIp)
							resolve(res.data)
						} else {
							uni.showToast({
								title: res.data.message,
								icon: "none"
							})
							reject()
						}
					}
				})
			})
		},
		getAppVersion({
			commit
		}, appVersion) {
			console.log("执行")
			return new Promise((resolve, reject) => {
				uni.request({
					url: "http://120.195.25.226:82/chne-totalbackstage/app/queryAppVersionWhetherLatest",
					method: "GET",
					data: {
						appVersion: appVersion
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
					},
					dataType: 'json',
					success: (res) => {
						console.log(res, 'test');
						if (res.data.status == 200) {
							console.log(res.data.object)
							// resolve(res.data)
							// commit("setUrl", res.data.object.serverIp)
							resolve(res.data)
						} else {
							uni.showToast({
								title: res.data.message,
								icon: "none"
							})
							reject()
						}
					}
				})
			})
		}

	}
})